#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

ll mpow(ll a, ll n, ll m) {//quick power;
    a %= m;
    if (a == 1 || n == 0) {
        return 1;
    }
    if (a == 0 || n == 1) {
        return a;
    }
    ll t = mpow(a, n / 2, m);
    t = t * t % m;
    if (n & 1) {
        t = t * a % m;
    }
    return t;
}

int main() {
    ll a, p, m;
    while (cin >> a >> p >> m) {
        if (a == 0 && p == 0 && m == 0)break;
        cout << mpow(a, p, m) << endl;
    }

    return 0;
}